package com.jackstraw.jack_base.datasource.moredatasource.bean;


import com.jackstraw.jack_base.datasource.database.parten.entity.bean.DataBaseFactory;
import com.jackstraw.jack_base.util.StringUtil;
import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.sql.SQLException;

public class C3p0DataBaseFactory extends DataBaseFactory<C3p0DataBaseEntity> {


    @Override
    protected DataSource dataSource(C3p0DataBaseEntity c3p0DataBaseEntity) {
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
        if (StringUtil.StringUtilsBylang3.isNotEmpty(c3p0DataBaseEntity.getDriverClass())) {
            try {
                comboPooledDataSource.setDriverClass(c3p0DataBaseEntity.getDriverClass());
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
        comboPooledDataSource.setJdbcUrl(c3p0DataBaseEntity.getJdbcUrl());
        comboPooledDataSource.setPassword(c3p0DataBaseEntity.getPassword());
        comboPooledDataSource.setCheckoutTimeout(c3p0DataBaseEntity.getValidationQueryTimeout());
        comboPooledDataSource.setInitialPoolSize(c3p0DataBaseEntity.getInitialSize());
        comboPooledDataSource.setMaxPoolSize(c3p0DataBaseEntity.getMaxActive());
        comboPooledDataSource.setMinPoolSize(c3p0DataBaseEntity.getMinIdle());
        comboPooledDataSource.setUser(c3p0DataBaseEntity.getUsername());
        comboPooledDataSource.setDataSourceName(c3p0DataBaseEntity.getName());

        try {
            comboPooledDataSource.softResetAllUsers();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return comboPooledDataSource;
    }
}
